<template>
  <div class="p-4">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-form inline>
          <el-form-item>
            <el-button
              type="success"
              icon="el-icon-plus"
              @click="deBounceWrap(handleAdd)"
              >新增</el-button
            >
          </el-form-item>
        </el-form>
      </div>
      <el-table :data="data" border>
        <el-table-column type="selection" width="55"> </el-table-column>
        <el-table-column prop="id" label="序号"></el-table-column>
        <el-table-column prop="type" label="类型">
          <template slot-scope="scope">
            <div v-if="scope.row.type === 1">真心话</div>
            <div v-if="scope.row.type === 2">大冒险</div>
          </template>
        </el-table-column>
        <el-table-column prop="title" label="题目"></el-table-column>
        <el-table-column prop="status" label="状态">
          <template slot-scope="scope">
            <el-tag type="success" v-if="scope.row.status === 1">显示</el-tag>
            <el-tag type="danger" v-if="scope.row.status === 0">隐藏</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="created_at" label="添加时间"></el-table-column>
        <el-table-column label="操作 ">
          <template slot-scope="scope">
            <el-button
              icon="el-icon-edit"
              type="primary"
              plain
              size="mini"
              @click="deBounceWrap(handleEdit, scope.row)"
            >
              编辑
            </el-button>
            <el-button
              icon="el-icon-delete"
              type="danger"
              plain
              size="mini"
              @click="deBounceWrap(handleDel, scope.row)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <cus-pagination
        :total="total"
        :page.sync="listQuery.page"
        :limit.sync="listQuery.page_size"
        @pagination="getList"
      />
    </el-card>
    <el-dialog
      :title="titleOptions[title]"
      :visible.sync="dialogVisible"
      :close-on-click-modal="false"
      width="40%"
      @close="handleClear"
      :before-close="handleClose"
    >
      <el-form :model="form" ref="form" :rules="rules" label-width="120px">
        <el-form-item prop="title_type" label="类型" label-width="120px">
          <el-select v-model="form.title_type" clearable placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="title_name" label="题目" label-width="120px">
          <el-input
            style="width:220px"
            type="textarea"
            v-model="form.title_name"
            onkeypress="javascript:if(event.keyCode === 32)event.returnValue = false;"
            placeholder="请输入题目"
          ></el-input>
        </el-form-item>
        <el-form-item prop="status" label="状态">
          <el-radio v-model="form.status" :label="1">显示</el-radio>
          <el-radio v-model="form.status" :label="0">隐藏</el-radio>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer" style="text-align: center">
        <el-button type="primary" plain @click="handleClose">返回</el-button>
        <el-button type="primary" @click="deBounceWrap(handleSumbit, 'form')"
          >提交</el-button
        >
      </div>
    </el-dialog>

    <!-- 删除弹框 -->
    <el-dialog
      title="确定删除吗?"
      :visible.sync="deleteDialogVisible"
      width="435px"
    >
      <span style=""
        ><em style="font-style: normal; color: red">删除后不可恢复</em
        >，你还要继续吗？</span
      >
      <div slot="footer" class="dialog-footer">
        <el-button @click="deleteDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirmDelete">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {
  TruthList,
  addTruth,
  editTruth,
  delTruth
} from "@/api/truthDare/truthDare";
export default {
  data() {
    return {
      data: [],
      options: [
        {
          value: 1,
          label: "真心话"
        },
        {
          value: 2,
          label: "大冒险"
        }
      ],
      listQuery: {
        page: 1,
        per_page: 10
      },
      total: 0,
      titleOptions: {
        add: "新增",
        edit: "编辑"
      },
      title: "add",
      rowData: {},
      form: {
        title_name: undefined,
        title_type: undefined,
        status: undefined
      },
      rules: {
        title_name: [
          { required: true, message: "请输入题目", trigger: "blur" },
          { max: 100, message: "题目长度最多不能超过100个字符" }
        ],
        title_type: [
          { required: true, message: "请选择类型", trigger: "change" }
        ],
        status: [{ required: true, message: "请选择状态", trigger: "change" }]
      },
      dialogVisible: false,
      //删除
      deleteDialogVisible: false,
      dataIds: undefined
    };
  },
  created() {
    this.getList();
  },
  methods: {
    //列表
    getList() {
      TruthList(this.listQuery).then(res => {
        this.data = res.data.data;
        this.total = res.data.total;
      });
    },
    handleAdd() {
      this.title = "add";
      this.dialogVisible = true;
    },
    handleEdit(row) {
      this.title = "edit";
      this.rowData = row;
      this.form.title_name = this.rowData.title;
      this.form.title_type = this.rowData.type;
      this.form.status = this.rowData.status;
      this.dialogVisible = true;
    },
    handleSumbit() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.title === "add") {
            let data = {
              title_name: this.form.title_name,
              title_type: this.form.title_type,
              status: this.form.status
            };

            addTruth(data).then(res => {
              if (res.code === 200) {
                this.$message.success("提交成功");
                this.getList();
                this.dialogVisible = false;
              } else {
              }
            });
          }
          if (this.title === "edit") {
            let data1 = {
              id: this.rowData.id,
              title_name: this.form.title_name,
              title_type: this.form.title_type,
              status: this.form.status
            };

            editTruth(data1).then(res => {
              if (res.code === 200) {
                this.$message.success("编辑成功");
                this.getList();
                this.dialogVisible = false;
              }
            });
          }
        } else {
          return false;
        }
      });
    },
    //清空表单
    handleClose() {
      this.dialogVisible = false;
    },
    handleClear() {
      this.$refs["form"].resetFields();
      this.$refs["form"].clearValidate();
      this.form = {
        title_name: undefined,
        title_type: undefined,
        status: undefined
      };
    },
    //删除
    handleDel(row) {
      this.dataIds = { id: `${row.id}` };
      this.deleteDialogVisible = true;
    },
    // 确定删除
    confirmDelete() {
      delTruth(this.dataIds).then(response => {
        if (response.code === 200) {
          this.getList();
          this.deleteDialogVisible = false;
          this.$notify({
            type: "success",
            message: "操作成功"
          });
        }
      });
    }
  }
};
</script>
<style></style>
